package com.sgj.completable;

import java.util.concurrent.*;

/**
 * @Author: Guoji Shen
 * @Date: 2021/8/10 14:28
 */
public class ThreadPoolTest {

    public static ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(3, 5, 5,
            TimeUnit.SECONDS, new LinkedBlockingDeque<Runnable>(10));

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println(test());
        // System.out.println(test2());
    }

    public static String test () throws ExecutionException, InterruptedException {
        CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            String result = "aaa";
            return result;
        }, poolExecutor);

        return completableFuture.get();
    }

    public static String test2 () throws ExecutionException, InterruptedException {
        CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, poolExecutor);

        String result = "aaa";
        return result;
    }
}